/*
The MIT License (MIT)
Copyright (c) 2013 Dalvik Boss dalvikboss@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package sidl.generator.java;

import sidl.Clazz;
import sidl.Field;
import sidl.Type;
import sidl.generator.GeneratorContext;

public class ObjectWriterGenerator extends JavaGeneraor {

    public ObjectWriterGenerator(GeneratorContext context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void doGenerateJava() throws Throwable {
        println("private static class ObjectWriter {");
        println("    ByteBuffer bb = ByteBuffer.allocate(PACKET_MAX);");
        println("    public ObjectWriter() throws SidlException {");
        println("        put(0xffffffff);//this is padding for the total packet length");
        println("    }");
        
        println("    void clear() throws SidlException {");
        println("        bb.clear();");
        println("        put(0xffffffff);");
        println("    }");
        println("    public void set(int index, int value) throws SidlException {");
        println("        try {");
        println("            bb.putInt(index, value);" );
        println("        }catch(Throwable t) {");
        println("            throw new SidlException(t);");
        println("        }");
        println("    }");

        
        for(String prim: Type.prims) {
            String putName = prim.replaceFirst(prim.substring(0, 1), prim.substring(0, 1).toUpperCase());
            println("    protected void put(%s i) throws SidlException {",prim);
            println("        try {");
            if(prim.equals("boolean")) {
                println("            bb.putInt(i?1:0);");
            } else if(prim.equals("byte")) {
                println("            bb.put(i);");
            } else {
                println("            bb.put%s(i);", putName);
            }
            println("        }catch(Throwable t) {");
            println("            throw new SidlException(t);");
            println("        }");
            println("    }");
            
            println("    protected void put(%s[] array) throws SidlException {",prim);
            println("        try {");
            println("            if(array==null) {");
            println("                bb.putInt(-1);");
            println("                return;");
            println("            }");
            println("            bb.putInt(array.length);");
            if(prim.equals("byte")) {
                println("            bb.put(array);", putName);                
            } else {
                println("            for(%s elem: array) {", prim);
                if(prim.equals("boolean")) {
                    println("                bb.putInt(elem?1:0);");
                } else {
                    println("                bb.put%s(elem);", putName);    
                }
                println("            }");
            }
            println("        } catch(Throwable t) {");
            println("            throw new SidlException(t);");
            println("        }");        
            println("    }");

        }
        
        
        println("    protected void put(String[] array) throws SidlException {");
        println("        try {");
        println("            if(array==null) {");
        println("                bb.putInt(-1);");
        println("                return;");
        println("            }");
        println("            bb.putInt(array.length);");
        println("            for(String elem: array) {");
        println("                put(elem);");
        println("            }");
        println("        } catch(Throwable t) {");
        println("            throw new SidlException(t);");
        println("        }");
        println("    }");
        
        println("    protected void put(String s) throws SidlException{");
        println("        try {");
        println("            if(s==null) {");
        println("                bb.putInt(-1);");
        println("                return;");
        println("            }");
        println("            byte[] data = s.getBytes(\"utf-8\");");
        println("            bb.putInt(data.length);");
        println("            bb.put(data);");
        println("        } catch(Throwable t) {");
        println("            throw new SidlException(t);");
        println("        }");
        println("    }");
        

        
        
        
        for(Clazz clz: doc.clazzes) {
            println("    protected void put(%s v) throws SidlException {", clz.name);
            println("        if(v==null) {");
            println("            put(-1);");
            println("            return;");
            println("        } else {");
            println("            put(1);");
            println("        }");
            for(Field f:  clz.fields) {
                println("        put(v.%s);", f.name);
            }
            println("    }");
        }
        
        for(Clazz clz: doc.clazzes) {
            println("    protected void put(%s[] array) throws SidlException {", clz.name);
            println("        if(array==null) {");
            println("            put(-1);");
            println("            return;");
            println("        }");
            println("        put(array.length);");
            println("        for(%s elem : array) {", clz.name);
            println("            put(elem);");
            println("        }");
            println("    }");
        }
        
        
        
        println("    protected void put_remote_exception(Throwable t) throws SidlException {");
        println("        if(t==null) {");
        println("            put((RemoteThrowable)null);;//no exception");
        println("            return;");
        println("        }");
        println("        ");
        println("        RemoteThrowable rt = new RemoteThrowable();");
        println("    ");
        println("        rt.message = t.getMessage()==null?\"no message\":t.getMessage();");
        println("        StackTraceElement[] stes = t.getStackTrace();");
        println("        if(stes!=null && stes.length > 0) {");
        println("            rt.stacks = new %s.RemoteStackTraceElement[stes.length];", doc.mainInf().name);
        println("            for(int i = 0; i < stes.length; i++) {");
        println("                rt.stacks[i] = new RemoteStackTraceElement();");
        println("                rt.stacks[i].className = stes[i].getClassName()==null?\"nothing\":stes[i].getClassName();");
        println("                rt.stacks[i].fileName = stes[i].getFileName()==null?\"nothing\":stes[i].getFileName();");
        println("                rt.stacks[i].methodName = stes[i].getMethodName()==null?\"nothing\":stes[i].getMethodName();");
        println("                rt.stacks[i].lineNumber = stes[i].getLineNumber();");
        println("            }");
        println("        }");
        println("        ");
        println("        put(rt);");
        println("    ");
        println("    ");
        println("       "); 
        println("    }     ");   
        println("}");                
    }

}
